<?php

/**
 * 新版本,每个咨询类型. 单独定价
 */

namespace app\v5\controller;

use think\Db;
use think\Exception;
use think\facade\Log;
use app\service\WxService;
use think\facade\Env;
use think\cache\driver\Redis;


class Index
{
    const INIT_USED_FLAG = 100;
    const OK_USED_FLAG = 200;
    /**
     * 临时活动,赠送(体质查询,单日)体验卡
     */
    public function tizhiPresent()
    {
        # 查看当前IP账号是否已经 used 同时 是否 活动已经结束
        $ip = $_SERVER['REMOTE_ADDR'];
        $cacheKeyUsed = $ip.'_used_day';
        $redis = new Redis();
        $cacheUsedKeyExists = $redis->handler()->exists($cacheKeyUsed);
        
        $res = [
            'code' => 0,
            'data' => ['is_over' => 1, 'is_used' => 1],
            'msg' => ''
        ];
        # 如果又代理免费标识: agentFree
        if (input('agentFree', 0) == 1) {
            $res['data']['is_used'] = static::OK_USED_FLAG;
            Log::info('代理免费::tizhiPresent::'. json_encode($res).'::params::'.json_encode(input('')));
            return json($res);
            exit();
        }
        if (time() > Env::get('app.tizhiPresentTimeStart') && time() < Env::get('app.tizhiPresentTimeEnd')) {
            $res['data']['is_over'] = 0;
            # 在活动期间, 如果没有$cacheKeyUsed, 则设置一个 初始领取值: 100
            if (!$cacheUsedKeyExists) {
                $redis->handler()->set($cacheKeyUsed, static::INIT_USED_FLAG);
                $res['data']['is_used'] = static::OK_USED_FLAG;
            }
        }
        # 如果$cacheKeyUsed, 存在. 比较是否有使用的日期
        if ($cacheUsedKeyExists) {
            $cacheUsedDay = $redis->handler()->get($cacheKeyUsed);
            
            if ($cacheUsedDay == static::INIT_USED_FLAG || $cacheUsedDay == date('Y-m-d')) $res['data']['is_used'] = static::OK_USED_FLAG;
        }
        Log::info('debug::tizhiPresent::'. json_encode($res).'::params::'.json_encode(input('')));
        return json($res);
    }
}